package com.zmj.sy.mom.srv.aps.config;

import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import com.zmj.sy.mom.srv.aps.utils.algorithm.ApsAlgorithm;
import com.zmj.sy.mom.srv.aps.utils.diff.DiffRule;
import com.zmj.sy.mom.srv.aps.utils.nest.NestRule;
import com.zmj.sy.mom.srv.aps.utils.order.ExternalOrder;
import com.zmj.sy.mom.srv.aps.utils.schedulerole.ScheduleRole;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Configuration
public class StrategyConfig {

    private final Map<String, ExternalOrder> externalOrderMap;

    private final Map<Integer, ScheduleRole> scheduleRoleMap;

    private final Map<String, ApsAlgorithm> apsAlgorithmMap;

    private final Map<String, DiffRule> diffRuleMap;

    private final Map<String, NestRule> nestRuleMap;

    public StrategyConfig(List<ExternalOrder> externalOrderList, List<ScheduleRole> scheduleRoleList, List<ApsAlgorithm> apsAlgorithmList, List<DiffRule> diffRuleList, List<NestRule> nestRuleList) {
        externalOrderMap = externalOrderList.stream().collect(Collectors.toMap(ExternalOrder::getMyCode, Function.identity()));
        scheduleRoleMap = scheduleRoleList.stream().collect(Collectors.toMap(ScheduleRole::getMyCode, Function.identity()));
        apsAlgorithmMap = apsAlgorithmList.stream().collect(Collectors.toMap(ApsAlgorithm::getCode, Function.identity()));
        diffRuleMap = diffRuleList.stream().collect(Collectors.toMap(DiffRule::getCode, Function.identity()));
        nestRuleMap = nestRuleList.stream().collect(Collectors.toMap(NestRule::getCode, Function.identity()));
    }

    public ExternalOrder getExternalOrder(String code) {
        ExternalOrder externalOrder = externalOrderMap.get(code);
        if(externalOrder == null){
            throw SyExceptionUtils.e("不存在的外部订单类型");
        }
        return externalOrder;
    }

    public ScheduleRole getScheduleRole(Integer code) {
        ScheduleRole scheduleRole = scheduleRoleMap.get(code);
        if(scheduleRole == null){
            throw SyExceptionUtils.e("不存在的排产策略");
        }
        return scheduleRole;
    }

    public ApsAlgorithm getApsAlgorithm(String code) {
        ApsAlgorithm externalOrder = apsAlgorithmMap.get(code);
        if(externalOrder == null){
            throw SyExceptionUtils.e("不存在的外部订单类型");
        }
        return externalOrder;
    }

    public DiffRule getDiffRule(String code) {
        DiffRule externalOrder = diffRuleMap.get(code);
        if(externalOrder == null){
            throw SyExceptionUtils.e("不存在的外部订单类型");
        }
        return externalOrder;
    }

    public NestRule getNestRule(String code) {
        NestRule externalOrder = nestRuleMap.get(code);
        if(externalOrder == null){
            throw SyExceptionUtils.e("不存在的套料厂商");
        }
        return externalOrder;
    }



}
